﻿using System.Windows.Forms;
using ShkSimulation.core.template;
using ShkSimulation.desktop.component.tools;
using ShkSimulation.carsharing.impl;
using ShkSimulation.carsharing.impl.elements;

namespace ShkSimulation.desktop.component {
    public sealed partial class SimulationControl : UserControl, ResetSimulation {

        private SimulationApplication application;
        private Network Network { get; set; }
		private NetworkResultHolder ResultHolder { get; set; }
		private NetworkEnvironment NetworkInputData { get; set; }
		
		public SimulationControl() {
			InitializeComponent();
		}

		public void Reset() {
			this.application = null;
			this.Network = null;
			this.NetworkInputData = null;
			this.ResultHolder = null;
			this.InstancePropertyView.SetInstance(null);
			this.NetworkControl.Reset();
		}

		public void SetSimulationApplication(SimulationApplication app) {
			this.application = app;
			NetworkControl.SetSimulationApplication(application);
			NetworkControl.OnStationClick += NetworkControlOnOnStationClick;
			NetworkControl.OnRoadClick += NetworkControlOnOnRoadClick;
			NetworkControl.OnNetworkClick += NetworkControlOnNetworkClick;
			InstancePropertyView.SetInstance(application.SimulationNetwork);
			application.AfterInitSuccess += this.AfterInitialize;
		}

		private void NetworkControlOnNetworkClick(Network obj) {
			if (!application.Running) {
				this.InstancePropertyView.SetInstance(obj);
			}
		}

		private void NetworkControlOnOnRoadClick(Road road) {
			if (!application.Running) {
				this.InstancePropertyView.SetInstance(road);
			}
		}

		private void NetworkControlOnOnStationClick(Station station) {
			if (!application.Running) {
				this.InstancePropertyView.SetInstance(station);
			}
		}

		private void AfterInitialize(SimulationInfo simulationInfo) {
			this.Network = application.SimulationNetwork;
			this.NetworkInputData = application.NetworkInputData;
			this.ResultHolder = application.ResultHolder;
		}

    }
}
